package com.kbt.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.*;
import com.android.volley.Request;
import com.kbt.adapter.AddressForMangerAdapter;
import com.kbt.base.BaseApplication;
import com.kbt.commont.Constants;
import com.kbt.global.data.ApplicationData;
import com.kbt.model.data.*;
import com.kbt.net.RequestManager;
import com.kbt.util.data.StaticData;
import com.kbt.util.tool.BtnSetting;
import com.kbt.util.tool.RestListViewSizeTool;
import com.kbt.util.utils.LoadingDialog;
import com.kbt.util.utils.SharePreferenceUtils;
import com.kbt.util.utils.Utils;

import java.io.Serializable;
import java.util.*;

/**
 * @Title: KBT_ANDRIOD
 * @Copyright: KJ Co., Ltd. Copyright 2015-2020,  All rights reserved
 * @Description: 管理收货地址的Activity
 * @author: zhgk
 * @data: 2015/8/14 8:41
 * @version: V1.0
 */
public class AddressMangeActivity extends Activity implements RequestManager.ResponseInterface{

    private ListView userAddressListView;
    private AddressForMangerAdapter adapter;
    private RequestManager mRequestManager;
    private Map<String, Object> map =null;
    private AddressListBean addressListBean;
    private List<AddressBean> addressBeans = new ArrayList<AddressBean>();
    private SharePreferenceUtils share;
    private LoadingDialog mProgress;
    public static String tag=null;
    public static List<ShopCarBean> goodInfoList;
    public static  String biaoji="";
    public static String order_jf_sub_je="";

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.address_manger);
        this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_return_btn);

        //设置返回按钮事件,关闭当前Activity，返回数据
        ImageView returnImg = (ImageView) findViewById(R.id.return_btn);
        returnImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (addressBeans.size() == 0) {
                    if(tag!=null&&tag.equals("1")){
                        Intent intent = new Intent(AddressMangeActivity.this, GoodForPayActivity.class);
                        intent.putExtra("list", (Serializable) goodInfoList);
                        intent.putExtra("biaoji", biaoji);
                        if(order_jf_sub_je!=null&&!"".equals(order_jf_sub_je)){
                        intent.putExtra("order_jf_sub_je",order_jf_sub_je);
                        }
                        startActivity(intent);
                    }
                    //没有地址了，清空数据
                    setResult(RESULT_OK, null);
                    BaseApplication.getInstance().removeActivity(AddressMangeActivity.this);
                    finish();
                } else {
                    if(tag!=null&&tag.equals("1")){
                        Intent intent = new Intent(AddressMangeActivity.this, GoodForPayActivity.class);
                        intent.putExtra("list", (Serializable) goodInfoList);
                        intent.putExtra("biaoji", biaoji);
                        if(order_jf_sub_je!=null&&!"".equals(order_jf_sub_je)){
                            intent.putExtra("order_jf_sub_je",order_jf_sub_je);
                        }
                        startActivity(intent);
                    }
                    //地址不变，保留原来地址
                    setResult(RESULT_CANCELED, null);
                    BaseApplication.getInstance().removeActivity(AddressMangeActivity.this);
                    finish();
                }

            }
        });

        TextView appTitleView = (TextView) findViewById(R.id.center_title);
        appTitleView.setSelected(false);
        appTitleView.setText("管理收货地址");
        share = new SharePreferenceUtils(AddressMangeActivity.this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mRequestManager = new RequestManager();
        mRequestManager.setResponseListener(this);
        map = new HashMap<String, Object>();
        initListView();
        initBottomBtn();
        initListViewData();
        tag = getIntent().getStringExtra("tag");
        biaoji = getIntent().getStringExtra("biaoji");
        order_jf_sub_je = getIntent().getStringExtra("order_jf_sub_je");
        goodInfoList = new ArrayList<ShopCarBean>();
        goodInfoList = (List<ShopCarBean>) getIntent().getSerializableExtra("list");
        BaseApplication.getInstance().addActivity(this);
    }

    /**
     * 初始化底部按钮
     */
    private void initBottomBtn() {
        Button btn = (Button) findViewById(R.id.newAddressBtn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent newIntent = new Intent(AddressMangeActivity.this, AddressAddActivity.class);
                newIntent.putExtra("biaoji", biaoji);
                newIntent.putExtra("list", (Serializable) goodInfoList);
                startActivityForResult(newIntent, StaticData.ADDRESS_FOR_MANGER_ADD);
                BaseApplication.getInstance().removeActivity(AddressMangeActivity.this);
            }
        });
    }

    /***
     * 初始化列表数据
     */
    private void initListViewData() {
        showProgress();
        if(Utils.isNetworkAvailable(AddressMangeActivity.this)) {
            mRequestManager.requestGet(Request.Method.GET, Constants.ServiceInterFace.selectAddressURL + share.getString("user_mobile", ""), Constants.RequestTag.selectAddressTag, map, AddressListBean.class);
        }else{
            stopProgress();
            Toast.makeText(AddressMangeActivity.this,R.string.Wangluo,Toast.LENGTH_SHORT).show();
        }
    }

    //初始化列表视图
    private void initListView() {
        userAddressListView = (ListView) findViewById(R.id.addressListView);
        adapter = new AddressForMangerAdapter(this, addressBeans);
        userAddressListView.setAdapter(adapter);
        RestListViewSizeTool.setListViewHeightBasedOnChildren(userAddressListView);
    }

    @Override
    public <T> void successResponse(T parsedGSON, int tag) {
        stopProgress();
        if (tag == Constants.RequestTag.selectAddressTag) {
            /** 判断返回值不为空 **/
            if (parsedGSON != null) {
                addressListBean = (AddressListBean) parsedGSON;
                if (addressListBean.getResult().equals("success")) {
                    if(addressListBean.getData().size()>0){
                       addressBeans =addressListBean.getData();
                        adapter.setList(addressBeans);
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        }
    }

    @Override
    public void errorResonse(String retmeg, int tag) {
        stopProgress();
        Toast.makeText(AddressMangeActivity.this,R.string.noWangluo,Toast.LENGTH_SHORT).show();
    }
    /**
     * progressDialog的展示
     */
    public void showProgress() {
        if (mProgress == null) {
            mProgress = new LoadingDialog(AddressMangeActivity.this,"");
            mProgress.setCancelable(true);
            mProgress.show();

        } else {
            if (mProgress.isShowing()) {
                stopProgress();
            } else {
                mProgress.show();
            }
        }
    }
    /**
     * progressDialog的消失
     */
    public void stopProgress() {
        if (mProgress != null) {
            mProgress.dismiss();
            mProgress = null;
        }
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            if(tag!=null&&tag.equals("1")){
                Intent intent = new Intent(AddressMangeActivity.this, GoodForPayActivity.class);
                intent.putExtra("list", (Serializable) goodInfoList);
                intent.putExtra("biaoji", biaoji);
                if(order_jf_sub_je!=null&&!"".equals(order_jf_sub_je)){
                    intent.putExtra("order_jf_sub_je",order_jf_sub_je);
                }
                startActivity(intent);
            }
            BaseApplication.getInstance().removeActivity(AddressMangeActivity.this);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
